package com.xdl.backstage.base.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xdl.backstage.base.bean.CompanyBean;
import com.xdl.backstage.base.bean.dto.CompanyDTO;
import com.xdl.backstage.base.entity.CompanyEntity;
import com.xdl.backstage.base.service.iface.CompanyService;
import com.xdl.moudle.utils.base.BaseController;
import com.xdl.moudle.utils.util.apibase.APIBaseResult;

@RestController
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {

	@Autowired
	private CompanyService companyService;
	
	@RequestMapping(value = "/save", method = RequestMethod.POST,  produces = "application/json")
	@ResponseBody
	public APIBaseResult save(CompanyBean[] arrBean) {
		try {
			return APIBaseResult.success(companyService.save(arrBean));
		} catch (Exception e) {
			return APIBaseResult.error(e.getMessage());
		}
	}
	
	@RequestMapping(value = "/getData", method = RequestMethod.GET, produces = "application/json")
	public APIBaseResult getData(String id) {
		try {
			CompanyEntity companyEntity = companyService.getData(id) ;
			if (companyEntity != null) {
				CompanyBean bean = new CompanyBean(companyEntity);
				return APIBaseResult.success(bean);
			}else {
				return APIBaseResult.success("");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return APIBaseResult.error(e.getMessage());
		}
	}
	
	@RequestMapping(value = "/queryData", method = RequestMethod.GET, produces = "application/json")
	public APIBaseResult queryData(CompanyDTO dto) {
		try {
			List<CompanyEntity> listData = companyService.queryDataByParams(dto);
			List<CompanyBean> resulstData  = new ArrayList<>();
			if (listData != null) {
				for (CompanyEntity companyEntity : listData) {
					CompanyBean bean = new CompanyBean(companyEntity);
					resulstData.add(bean);
				}
			}
			return APIBaseResult.success(resulstData);
		} catch (Exception e) {
			e.printStackTrace();
			return APIBaseResult.error(e.getMessage());
		}
	}
}
